﻿/*===================================================
* 类名称: Init
* 类描述:
* 创建人: oream
* 创建时间: 2017/12/24 21:12:13
* 修改人: 
* 修改时间:
* 版本： @version 1.0
=====================================================*/



using MC;
using System;
using System.Reflection;
using UnityEngine;

namespace Model
{
    public class Init : MonoBehaviour
    {
        public static Init Instance;

        public int Money;

        public ILRuntime.Runtime.Enviorment.AppDomain AppDomain;

        public string URL = "";

        public IStaticMetod start;

        public IStaticMetod update;

        public IStaticMetod lateUpdate;

        public IStaticMetod fixedUpdate;

        public IStaticMetod onApplicationQuit;

        private void Awake()
        {
            Instance = this;
            DontDestroyOnLoad(this);
        }

        private void Start()
        {
            ABHepler.DownloadBundle(URL, () =>
            {
                Debug.Log("所有资源最新");

#if ILR
                AppDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
                DllHelper.LoadHotfixAssembly();
                ILRuntime.CLR.TypeSystem.IType type = AppDomain.GetType("Hotfix.Init");
                Debug.Log("手动断点");
                OnHorFixLoaded();
                if (type == null)
                {
                    throw new Exception("发生不可逆转的错误!!!!");
                }
                this.start = new ILRStaticMethod(type, "Start");
                this.update = new ILRStaticMethod(type, "Update");
                this.onApplicationQuit = new ILRStaticMethod(type, "OnApplicationQuit");
                this.lateUpdate = new ILRStaticMethod(type, "LateUpdate");
                this.fixedUpdate = new ILRStaticMethod(type, "FixedUpdate");
#else
                Assembly mono = DllHelper.LoadHotfixAssembly();
                Type type = mono.GetType("Hotfix.Init");
                if (type == null)
                {
                    throw new Exception("发生不可逆转的错误!!!!");
                }
                this.start = new MonoStaticMethod(type, "Start");
                this.update = new MonoStaticMethod(type, "Update");
#endif
                this.start.Run();
                ABHepler.CloseLoadResourcesUpdataView();
            });
        }


        private void Update()
        {
            update?.Run();
        }
        private void FixedUpdate()
        {
            fixedUpdate?.Run();
        }

        private void LateUpdate()
        {
            lateUpdate?.Run();
        }

        private void OnApplicationQuit()
        {
            this.onApplicationQuit?.Run();
        }

        void OnHorFixLoaded()
        {

            #region 委托适配器

            AppDomain.DelegateManager.RegisterDelegateConvertor<UnityEngine.Events.UnityAction>((act) =>
            {
                return new UnityEngine.Events.UnityAction(() =>
                {
                    ((System.Action)act)();
                });
            });
            //AppDomain.DelegateManager.RegisterDelegateConvertor<AppDomain>((act) =>
            //{
            //    return new ThreadStart(() =>
            //    {
            //        ((System.Action)act)();
            //    });
            //});
            AppDomain.DelegateManager.RegisterMethodDelegate<UnityEngine.Object>();
            AppDomain.DelegateManager.RegisterMethodDelegate<Message>();
            AppDomain.RegisterCrossBindingAdaptor(new IAsyncStateMachineClassInheritanceAdaptor());
            LitJson.JsonMapper.RegisterILRuntimeCLRRedirection(AppDomain);

            #endregion

            // StartCoroutine(Test());

        }
    }
}